package com.group2.view;

import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Vector;
import model.*;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import static model.netBase.IP;
import model.Course;
import com.group2.util.*;



/*学生选课系统模块：
 * 查看我的课表*/

public class CourseTableFrame extends javax.swing.JFrame {

	private JPanel contentPane;
	private GridBagTable courseTable;
	private JScrollPane jScrollPane1;
	private static ArrayList<Course>list;
	private static String studentId;

	public CourseTableFrame(String id, Timer time) throws IOException, ClassNotFoundException {
		initComponents();
		studentId=id;
		time.start();
		this.addMouseMotionListener(new MouseMotionListener() {@Override public void mouseDragged(MouseEvent e) {}@Override public void mouseMoved(MouseEvent e) {time.stop();}});
		this.addMouseListener(new MouseListener() {@Override public void mouseClicked(MouseEvent e) {}@Override public void mousePressed(MouseEvent e) {}@Override public void mouseReleased(MouseEvent e) {}@Override public void mouseEntered(MouseEvent e) {time.start();}@Override public void mouseExited(MouseEvent e) {time.start();}});
		System.out.println("开始传输");
		//拉取选课队列
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("42" + '\n'+studentId+'\n');
		bw.flush();
		//从服务端输入到本地
		InputStream is = socket.getInputStream();
		ObjectInputStream in = new ObjectInputStream(is);
		Object obj = in.readObject();
		System.out.println("传输结束");
		list = (ArrayList<Course>) obj;
		//输出输入流关闭
		is.close();
		os.close();
		socket.close();
		//填写表格
		resetTable(list);
		//this.fillTable(list);
	}

	//根据该学生选课信息更新课表
	private void fillTable(ArrayList<Course> course) {
		DefaultTableModel dtm = (DefaultTableModel) courseTable.getModel();
		dtm.setRowCount(0);
		try {
			int size = course.size();
			int index = 0;
			while (size > 0) {
				Vector v = new Vector();v.add(course.get(index).getCourseId());v.add(course.get(index).getCourseName());v.add(course.get(index).getCourseTime().toString());v.add(course.get(index).getCourseLocation());v.add(course.get(index).getCourseTeacher());
				dtm.addRow(v);
				size--;
				index++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//根据一条课程内容向合并单元格中填写
	public void resetTable (ArrayList<Course>courses){

		DefaultTableModel tableModel=(DefaultTableModel) courseTable.getModel();

		for(int i=0;i<courses.size();++i) {
			int cW = courses.get(i).getCourseTime_Week();
			int cB = courses.get(i).getCourseTime_Begin();
			int cE = courses.get(i).getCourseTime_End();
			//填写课程名称，地点，老师
			String content = courses.get(i).getCourseName() + " " + courses.get(i).getCourseLocation() + " " + courses.get(i).getCourseTeacher();
			courseTable.mergeCells(cB - 1, cE - 1, cW + 1, cW + 1);//合并单元格
			courseTable.setValueAt(content, cB - 1, cW + 1);//第0行和第0列不可编辑
			courseTable.repaint();
		}
	}



	private void initComponents() {
		Font song = new Font("宋体", Font.PLAIN, 14);

		this.setBounds(150,50,800, 700);
		this.setResizable(false);
		contentPane=new JPanel();
		contentPane.setBounds(150,50,795,695);
		contentPane.setBorder(javax.swing.BorderFactory.createTitledBorder("我的课程"));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		jScrollPane1 = new JScrollPane();
		jScrollPane1.setBounds(16, 20, 760, 680);

		setTitle("查看我的课表");


		DefaultTableModel tm=new DefaultTableModel(
				new Object[][] {
						{"     1"},{"     2"},{"     3"},{"     4"},{"     5"},{"     6"},{"     7"},{"     8"},{"     9"},{"     10"},{"     11"},{"     12"},{"     13"},
				},
				new String[] {
						"周次/课程","周一", "\u5468\u4E00", "\u5468\u4E8C", "\u5468\u4E09", "\u5468\u56DB", "\u5468\u4E94", "\u5468\u516D", "\u5468\u65E5"
				}) {
			public boolean isCellEditable(int rowIndex, int columnIndex) {
				//第一列不可编译
				if(0 <= rowIndex && rowIndex < 14&& columnIndex == 0){
					return false;
				}else{
					return true;
				}

			}};

		courseTable = new GridBagTable(tm);
		DefaultTableCellRenderer tcr=new DefaultTableCellRenderer();
		tcr.setHorizontalAlignment(SwingConstants.CENTER);
		//tcr.setVerticalAlignment(SwingConstants.CENTER);
		courseTable.setRowHeight(45);
		courseTable.setFont(song);
		courseTable.setDefaultRenderer(getClass(), tcr);

		jScrollPane1.setViewportView(courseTable);
		contentPane.add(jScrollPane1);

		/*测试合并单元格！*/
		Course test_course=new Course("11","工科数学分析", 5, 1, 4,"程全新", "J8-303", 100);


	}





}